# Maintainer: Rémi BERTHO <remi.bertho@openmailbox.org>

_realname=muparser
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.4
pkgrel=1
pkgdesc="A fast math parser library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://beltoforion.de/en/muparser/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/beltoforion/muparser/archive/v${pkgver}.tar.gz
        "001-exports.patch")
sha256sums=('0c3fa54a3ebf36dda0ed3e7cd5451c964afbb15102bdbcba08aafb359a290121'
            'f125ce80b250e59a6d650cfecc11f511703a7e5079409bc287f43b3fd56cdc37')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-exports.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi


  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe || true
}

package() {
  cd "build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  # license
  install -D -m644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
